package xcmg.device.web.company;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.infra.AuthCode;
import xcmg.device.infra.BasicController;
import xcmg.device.service.company.EmployeeService;
import xcmg.device.service.vo.agent.ServiceBasicDataAgentVO;
import xcmg.device.service.vo.company.EmployeeVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.constants.ErrorCode;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.util.ArrayList;
import java.util.List;

/**
 * Employee Controller 实现类
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController extends BasicController {
    private static final Logger logger = LoggerFactory.getLogger(EmployeeController.class);
    @Autowired
    private EmployeeService employeeService;


    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiOperation(value = "供应商职工表分页列表信息")
    @FastMappingInfo(needLogin = true)
    public ActionResult<PageInfo<EmployeeVO>> list(@RequestBody SearchCommonVO<EmployeeVO> condition) throws Exception {
        if (null == condition.getFilters()) {
            condition.setFilters(new EmployeeVO());
        }
        if (StringUtils.isBlank(condition.getFilters().getFactoryOrgId())) {
            String orgId = getOrgId();
            condition.getFilters().setQueryOrgId(orgId);
        }
        condition.getFilters().setIsDel(0);
        PageCommonVO<EmployeeVO> pageCommonVO = employeeService.queryVoList(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }


    @RequestMapping(value = "/jobs", method = RequestMethod.GET)
    @ApiOperation(value = "供应商职工岗位信息列表")
    @FastMappingInfo(needLogin = true)
    public ActionResult<List<ServiceBasicDataAgentVO>> list(String orgId) throws Exception {
        List<ServiceBasicDataAgentVO> ll = employeeService.queryJobList(orgId);
        return actionValueResult(ll);
    }


    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true, code = AuthCode.Employee.ADD)
    @ApiOperation(value = "供应商职工表-保存")
    public ActionResult<ErrorCode> save(@RequestBody EmployeeVO employeeVO) {
        if (null == employeeVO) {
            return actionValueResult(ErrorCode.IllegalArument);
        }
        try {
            ErrorCode code = employeeService.save(employeeVO, getUserId(), getUserName(), getOrgId());
            return actionValueResult(code);
        } catch (Exception e) {
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/batchSave", method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true, code = AuthCode.Employee.ADD)
    @ApiOperation(value = "供应商职工表-保存")
    public ActionResult<ErrorCode> batchSave(@RequestBody List<EmployeeVO> list) {
        if (CollectionUtils.isEmpty(list)) {
            return actionValueResult(ErrorCode.IllegalArument);
        }
        try {
            ErrorCode code = employeeService.batchSave(list, getUserId(), getUserName(), getOrgId());
            return actionValueResult(code);
        } catch (Exception e) {
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }


    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiOperation("删除")
    @FastMappingInfo(needLogin = true, code = AuthCode.Employee.DEL)
    public ActionResult<ErrorCode> delete(@RequestBody ArrayList<String> ids) {
        try {
            return actionValueResult(employeeService.delete(ids, getOrgId()));
        } catch (Exception e) {
            return new ActionResult(500, e.getMessage());
        }
    }
}
